<?php

namespace app\api\Logic\Course;

use app\api\Logic\CampusCostLogic;
use app\api\Logic\Goods\CampusGoodsLogic;
use app\api\model\CampusCourseManagementPackageSublevelModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;

class ManagementPackageSublevelLogic
{
    /**
     * 根据套餐ID返回套餐内物品列表
     * @param $packageId
     * @return array
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function getSublevelByPackageId($packageId){
        $goodsLogic = new CampusGoodsLogic();
        $managementLogic = new ManagementLogic();
        $costLogic = new CampusCostLogic();

        $list = CampusCourseManagementPackageSublevelModel::where('p_id',$packageId)
            ->field('id,type,gs_id,pricing,num,total_price,giving,giving_type,derate_type,discount,subtotal_price')
            ->select();
        $list = objToArray($list) ?? [];

        foreach ($list as $key => $val){
            if($val['type'] == 1){
                //物品信息
                $list[$key]['goods_info'] = $goodsLogic->goodsInfo($val['gs_id']);
            }else if($val['type'] == 2){
                //费用信息
                $list[$key]['goods_info'] = $costLogic->costInfo($val['gs_id']);
            }else if($val['type'] == 3){
                //课程信息
                $list[$key]['goods_info'] = $managementLogic->managementInfo($val['gs_id']);
            }
        }

        return $list;
    }
}
